window.onload = async function () {
  try {
    const res = await axios({
      url: '/dashboard',
    })
    // console.log('res:', res)
    const { overview, year, salaryData, groupData, provinceData } = res.data
    // console.log(provinceData)
    for (let k in overview) {
      document.querySelector(`[name=${k}]`).innerHTML = overview[k]
    }

    // 薪资走势
    !(function () {
      const myChart = echarts.init(document.getElementById('line'))
      const option = {
        title: {
          text: '2021全学科薪资走势',
          top: '2%',
          left: '3%',
        },
        xAxis: {
          type: 'category',
          data: year.map((item) => item.month),
          axisLine: {
            lineStyle: {
              type: 'dashed',
              color: '#d3d3d3',
            },
          },
          axisLabel: {
            color: '#000',
          },
        },
        yAxis: {
          type: 'value',
          splitLine: {
            lineStyle: {
              color: '#53bdf9',
              type: 'dashed',
            },
          },
        },
        tooltip: {
          show: true,
        },
        series: [
          {
            data: year.map((item) => item.salary),
            type: 'line',
            smooth: true,
            symbolSize: 10,
            lineStyle: {
              width: 8,
              color: '#6790f7',
            },
            areaStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: '#cee3fc', // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    color: '#fff', // 100% 处的颜色
                  },
                ],
                global: false, // 缺省为 false
              },
            },
          },
        ],
      }
      myChart.setOption(option)
    })()

    // 薪资分布
    !(function () {
      const myChart = echarts.init(document.getElementById('salary'))
      const option = {
        title: {
          text: '班级薪资分布',
          top: '2%',
          left: '3%',
        },
        tooltip: {
          trigger: 'item',
        },
        legend: {
          bottom: '5%',
          left: 'center',
        },
        color: ['#f1a545', '#5f98f7', '#6accfa', '#69cf9e'],
        series: [
          {
            name: '薪资分布',
            type: 'pie',
            radius: ['55%', '75%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 12,
              borderColor: '#fff',
              borderWidth: 2,
            },
            label: {
              show: false,
              position: 'center',
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 40,
                fontWeight: 'bold',
              },
            },
            labelLine: {
              show: false,
            },
            data: salaryData.map((item) => {
              return {
                name: item.label,
                value: item.g_count + item.b_count,
              }
            }),
            //   [
            //   { value: 1048, name: 'harry' },
            //   { value: 735, name: 'Direct' },
            //   { value: 580, name: 'Email' },
            //   { value: 484, name: 'Union Ads' },
            // ],
          },
        ],
      }
      myChart.setOption(option)
    })()

    // 班级每组薪资
    !(function () {
      const myChart = echarts.init(document.getElementById('lines'))
      const option = {
        xAxis: {
          type: 'category',
          data: groupData[1].map((item) => item.name),
          axisLine: {
            lineStyle: {
              type: 'dashed',
              color: '#d4d4d6',
            },
          },
        },
        tooltip: {
          show: true,
        },
        yAxis: {
          type: 'value',
          splitLine: {
            lineStyle: {
              type: 'dashed',
            },
          },
        },
        color: [
          {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              {
                offset: 0,
                color: '#6ccfa1', // 0% 处的颜色
              },
              {
                offset: 1,
                color: '#d5f2e7', // 100% 处的颜色
              },
            ],
            global: false, // 缺省为 false
          },
          {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              {
                offset: 0,
                color: '#61a1e7', // 0% 处的颜色
              },
              {
                offset: 1,
                color: '#d8e8f8', // 100% 处的颜色
              },
            ],
            global: false, // 缺省为 false
          },
        ],
        series: [
          {
            data: groupData[1].map((item) => item.hope_salary),
            type: 'bar',
          },
          {
            data: groupData[1].map((item) => item.salary),
            type: 'bar',
          },
        ],
      }
      myChart.setOption(option)

      // 点击功能
      const btns = document.querySelectorAll('#btns .btn-xs')
      btns.forEach((item) => {
        item.addEventListener('click', function () {
          document.querySelector('.btn-blue').classList.remove('btn-blue')
          this.classList.add('btn-blue')
          // 获取当前点击的是哪个按钮
          const index = this.innerText.trim()
          // 给option重新设置数据
          option.xAxis.data = groupData[index].map((item) => item.name)
          option.series[0].data = groupData[index].map(
            (item) => item.hope_salary
          )
          option.series[1].data = groupData[index].map((item) => item.salary)
          myChart.setOption(option)
        })
      })
    })()

    // 男女薪资分布
    !(function () {
      const myChart = echarts.init(document.getElementById('gender'))
      const option = {
        title: [
          {
            text: '男女薪资分布',
            left: '3%',
            top: '2%',
          },
          {
            text: '男生',
            left: '50%',
            top: '47%',
            textAlign: 'center',
          },
          {
            text: '女生',
            left: '50%',
            top: '90%',
            textAlign: 'center',
          },
        ],
        tooltip: {
          trigger: 'item',
        },
        series: [
          {
            name: '男生',
            type: 'pie',
            radius: ['25%', '35%'],
            center: ['50%', '30%'],
            color: ['#f1a545', '#5f98f7', '#6accfa', '#69cf9e'],
            data: salaryData.map((item) => ({
              value: item.b_count,
              name: item.label,
            })),
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)',
              },
            },
          },
          {
            name: '女生',
            type: 'pie',
            radius: ['25%', '35%'],
            center: ['50%', '72%'],
            data: salaryData.map((item) => ({
              value: item.g_count,
              name: item.label,
            })),
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)',
              },
            },
          },
        ],
      }
      myChart.setOption(option)
    })()

    // 籍贯分布
    !(function () {
      const myChart = echarts.init(document.getElementById('map'))
      const dataList = [
        { name: '南海诸岛', value: 0 },
        { name: '北京', value: 0 },
        { name: '天津', value: 0 },
        { name: '上海', value: 0 },
        { name: '重庆', value: 0 },
        { name: '河北', value: 0 },
        { name: '河南', value: 0 },
        { name: '云南', value: 0 },
        { name: '辽宁', value: 0 },
        { name: '黑龙江', value: 0 },
        { name: '湖南', value: 0 },
        { name: '安徽', value: 0 },
        { name: '山东', value: 0 },
        { name: '新疆', value: 0 },
        { name: '江苏', value: 0 },
        { name: '浙江', value: 0 },
        { name: '江西', value: 0 },
        { name: '湖北', value: 0 },
        { name: '广西', value: 0 },
        { name: '甘肃', value: 0 },
        { name: '山西', value: 0 },
        { name: '内蒙古', value: 0 },
        { name: '陕西', value: 0 },
        { name: '吉林', value: 0 },
        { name: '福建', value: 0 },
        { name: '贵州', value: 0 },
        { name: '广东', value: 0 },
        { name: '青海', value: 0 },
        { name: '西藏', value: 0 },
        { name: '四川', value: 0 },
        { name: '宁夏', value: 0 },
        { name: '海南', value: 0 },
        { name: '台湾', value: 0 },
        { name: '香港', value: 0 },
        { name: '澳门', value: 0 },
      ]
      provinceData.forEach((item) => {
        const findObj = dataList.find((v) => {
          // 判断 item.name 是不是以dataList当前遍历项的name开头
          // 如果是，就找到了
          return item.name.startsWith(v.name)
        })
        findObj && (findObj.value = item.value)
      })
      console.log(dataList)

      const option = {
        title: {
          text: '籍贯分布',
          top: 10,
          left: 10,
          textStyle: {
            fontSize: 16,
          },
        },
        tooltip: {
          trigger: 'item',
          formatter: '{b}: {c} 位学员',
          borderColor: 'transparent',
          backgroundColor: 'rgba(0,0,0,0.5)',
          textStyle: {
            color: '#fff',
          },
        },
        visualMap: {
          min: 0,
          max: 6,
          left: 'left',
          bottom: '20',
          text: ['6', '0'],
          inRange: {
            color: ['#ffffff', '#0075F0'],
          },
          show: true,
          left: 40,
        },
        geo: {
          map: 'china',
          roam: false,
          zoom: 1.0,
          label: {
            normal: {
              show: true,
              fontSize: '10',
              color: 'rgba(0,0,0,0.7)',
            },
          },
          itemStyle: {
            normal: {
              borderColor: 'rgba(0, 0, 0, 0.2)',
              color: '#e0ffff',
            },
            emphasis: {
              areaColor: '#34D39A',
              shadowOffsetX: 0,
              shadowOffsetY: 0,
              shadowBlur: 20,
              borderWidth: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)',
            },
          },
        },
        series: [
          {
            name: '籍贯分布',
            type: 'map',
            geoIndex: 0,
            data: dataList,
          },
        ],
      }
      myChart.setOption(option)
    })()
  } catch (err) {
    console.log(err)
  }
}
